package nc.impl.hr.comp.attachment;

import java.util.ArrayList;
import java.util.Collection;
import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.itf.hr.comp.IAttachment;
import nc.vo.hr.comp.attachment.AttachmentVO;
import nc.vo.pub.BusinessException;

public class AttachmentImpl
  implements IAttachment
{
  BaseDAO baseDAO = new BaseDAO();

  public void addViewTimes(AttachmentVO[] attachmentVOs)
    throws DAOException
  {
    if ((attachmentVOs == null) || (attachmentVOs.length == 0))
    {
      return;
    }

    for (AttachmentVO attachmentVO : attachmentVOs)
    {
      attachmentVO.setView_times(Integer.valueOf(attachmentVO.getView_times() == null ? 1 : attachmentVO.getView_times().intValue() + 1));
    }

    this.baseDAO.updateVOArray(attachmentVOs, new String[] { "view_times" });
  }

  public void deleteAttachment(String strPk_attachment)
    throws BusinessException
  {
    if ((strPk_attachment == null) || (strPk_attachment.trim().length() == 0))
    {
      return;
    }

    this.baseDAO.deleteByPK(AttachmentVO.class, strPk_attachment);
  }

  public void deleteAttachment(String strPk_object, String strObjectType)
    throws BusinessException
  {
    if (strPk_object == null)
    {
      return;
    }

    String strCondition = "pk_object='" + strPk_object + "'";

    if (strObjectType != null)
    {
      strCondition = strCondition + " and object_type='" + strObjectType + "'";
    }

    this.baseDAO.deleteByClause(AttachmentVO.class, strCondition);
  }

  private String[] getAttrExceptAttachment()
  {
    AttachmentVO attachmentVO = new AttachmentVO();

    ArrayList arrListAttr = new ArrayList();

    for (String strAttrName : attachmentVO.getAttributeNames())
    {
      if (!"attachment".equals(strAttrName))
      {
        arrListAttr.add(strAttrName);
      }
    }
    return (String[])arrListAttr.toArray(new String[0]);
  }

  public byte[] queryAttachment(String strPk_Attachment)
    throws BusinessException
  {
    AttachmentVO attachmentVO = (AttachmentVO)this.baseDAO.retrieveByPK(AttachmentVO.class, strPk_Attachment, new String[] { "attachment", "view_times" });

    if (attachmentVO == null)
    {
      return null;
    }

    addViewTimes(new AttachmentVO[] { attachmentVO });

    return attachmentVO.getAttachment();
  }

  public AttachmentVO[] queryAttachment(String strPk_Object, String strObjectType)
    throws BusinessException
  {
    if (strPk_Object == null)
    {
      return null;
    }

    String strCondition = "pk_object='" + strPk_Object + "'";

    if (strObjectType != null)
    {
      strCondition = strCondition + " and object_type='" + strObjectType + "'";
    }

    Collection collection = this.baseDAO.retrieveByClause(AttachmentVO.class, strCondition, new String[] { "attachment", "view_times" });

    AttachmentVO[] attachmentVOs = (AttachmentVO[])collection.toArray(new AttachmentVO[0]);

    addViewTimes(attachmentVOs);

    return attachmentVOs;
  }

  public AttachmentVO[] queryAttachmentInfo(String strPk_Object, String strObjectType)
    throws BusinessException
  {
    if (strPk_Object == null)
    {
      return null;
    }

    String strCondition = "pk_object='" + strPk_Object + "'";

    if (strObjectType != null)
    {
      strCondition = strCondition + " and object_type='" + strObjectType + "'";
    }

    Collection collection = this.baseDAO.retrieveByClause(AttachmentVO.class, strCondition, getAttrExceptAttachment());

    return (AttachmentVO[])collection.toArray(new AttachmentVO[0]);
  }

  public AttachmentVO updateAttachmentInfo(AttachmentVO attachmentVO)
    throws BusinessException
  {
    this.baseDAO.updateVO(attachmentVO, getAttrExceptAttachment());

    return attachmentVO;
  }

  public AttachmentVO upload(AttachmentVO attachmentVO)
    throws BusinessException
  {
    if (attachmentVO == null)
    {
      return null;
    }

    switch (attachmentVO.getStatus())
    {
    case 3:
      this.baseDAO.deleteByPK(AttachmentVO.class, attachmentVO.getPrimaryKey());
      break;
    case 2:
      attachmentVO.setPrimaryKey(this.baseDAO.insertVO(attachmentVO));
      attachmentVO.setAttachment(null);
      break;
    }

    return attachmentVO;
  }
}